package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
    implements BaseAttrInfoService{

    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueService baseAttrValueService;

    @Override
    public List<BaseAttrInfo> getBaseAttrInfoAndValue(Long c1id, Long c2id, Long c3id) {
        List<BaseAttrInfo> result = baseAttrInfoMapper.getBaseAttrInfoAndValue(c1id,c2id,c3id);
        return result;
    }

    /**
     * 新增
     * @param baseAttrInfo
     */
    @Override
    public void insertAttr(BaseAttrInfo baseAttrInfo) {
        //1.保存平台属性名(直接保存)
        this.save(baseAttrInfo);
        //获取属性名的id(保存成功之后会自动将id回填)
        Long id = baseAttrInfo.getId();

        //2.保存平台属性值
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        for (BaseAttrValue attrValue : attrValueList) {
            //每个属性值id回填
            attrValue.setAttrId(id);
        }

        baseAttrValueService.saveBatch(attrValueList);

    }

    /**
     * 修改属性值时,
     * 对于List<BaseAttrValue>中的id,不在前端发来的数据集合被先删除
     * 剩下的BaseAttrValue对象有id则修改,没有则新增
     * @param baseAttrInfo
     */
    @Override
    public void updateAttr(BaseAttrInfo baseAttrInfo) {
        //1.修改属性名表
        baseAttrInfoMapper.updateById(baseAttrInfo);
        //2.修改属性值表
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();

        //获取前端发来的所有属性值id
        List<Long> ids = attrValueList.stream()
                .filter(item -> item.getId() != null)
                .map(item -> item.getId())
                .collect(Collectors.toList());
        //防止全删没有ids集合造成sql错误
        if(ids.size() > 0){
            //删除部分属性值
            QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
            wrapper.eq("attr_id",baseAttrInfo.getId());
            wrapper.notIn("id",ids);
            baseAttrValueService.remove(wrapper);
        }else{
            //删除所有的属性值
            QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
            wrapper.eq("attr_id",baseAttrInfo.getId());
            baseAttrValueService.remove(wrapper);
        }

        //遍历数据集合,有id就修改,没有id就新增
        attrValueList.stream()
                .forEach(item -> {
                    if(item.getId() != null){
                        baseAttrValueService.updateById(item);
                    }else{
                        item.setAttrId(baseAttrInfo.getId());
                        baseAttrValueService.save(item);
                    }
                });
    }
}




